// vue.config.js
const isProduction = process.env.NODE_ENV === "production";
const cdn = {
  css: [
    "https://cdn.jsdelivr.net/npm/element-ui@2.10.1/lib/theme-chalk/index.css",
    "https://cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.css"
  ],
  js: [
    "https://cdn.jsdelivr.net/npm/vue@2.6.10/dist/vue.runtime.min.js",
    "https://cdn.jsdelivr.net/npm/vue-router@3.0.3/dist/vue-router.min.js",
    "https://cdn.jsdelivr.net/npm/vuex@3.0.1/dist/vuex.min.js",
    "https://cdn.jsdelivr.net/npm/element-ui@2.10.1/lib/index.js",
    "https://cdn.jsdelivr.net/npm/axios@0.19.0/dist/axios.min.js",
    "https://cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.js"
  ]
};
module.exports = {
  chainWebpack: config => {
    // 生产环境配置
    if (isProduction) {
      // 移除 prefetch 插件
      config.plugins.delete("prefetch");
      // 移除 preload 插件
      config.plugins.delete("preload");
      // 生产环境注入cdn
      config.plugin("html").tap(args => {
        args[0].cdn = cdn;
        return args;
      });
    }
  },
  configureWebpack: config => {
    if (isProduction) {
      // 用cdn方式引入
      config.externals = {
        vue: "Vue",
        vuex: "Vuex",
        "vue-router": "VueRouter",
        "element-ui": "ELEMENT",
        axios: "axios",
        nprogress: "NProgress"
      };
    }
  },
  devServer: {
    port: 8480,
    disableHostCheck: true,
    proxy: {
      "/api": {
        target: "http://toroke.imwork.net:7777",
        changeOrigin: true // 开启代理
      },
      "/ws": {
        target: "ws://im-api.easemob.com",
        ws: true,
        secure: false
      }
    }
  },
  productionSourceMap: false
};
